/**
 * 
 */
package database.berkeleydb;

import java.io.File;
import java.util.Iterator;

import com.sleepycat.je.Database;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;

/**
 * @author LionBule
 * 2011-5-12 ����10:39:28
 */
public class TestBdbPref {
	public static void main(String[] args) {
		File file = new File("/home/db");
		if(!file.exists()){
			file.mkdirs();
		}
		EnvironmentConfig envConfig = new EnvironmentConfig();
		envConfig.setAllowCreate(true);
		envConfig.setCacheSize(0);
		envConfig.setTransactional(true);
		
		envConfig.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, String.valueOf(1024*1024*20));
		envConfig.setConfigParam(EnvironmentConfig.LOG_FAULT_READ_SIZE, String.valueOf(1024*4));
		envConfig.setConfigParam(EnvironmentConfig.ENV_BACKGROUND_WRITE_LIMIT, String.valueOf(5000));
		
		Environment env = new Environment(file, envConfig);
        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setAllowCreate(true);
        storeConfig.setTransactional(true);
        
        EntityStore store = new EntityStore(env, "thead", storeConfig);
        
        PrimaryIndex<Integer, TestEntity> primaryIndex = store.getPrimaryIndex(Integer.class, TestEntity.class);
        
        System.out.println("count: "+primaryIndex.count());
        
        long start = System.currentTimeMillis();
        for(int i=0;i<500000;i++){
        	primaryIndex.putNoReturn(new TestEntity(i));
        }
        System.out.println(System.currentTimeMillis()-start);
        
        
//        EntityCursor<Integer> ec = primaryIndex.keys();
//        Iterator<Integer> list = ec.iterator();
//        ec.dup();
//        ec.first();
//        ec.next();
//        System.out.println(ec.count());
//        while(list.hasNext()){
//        	System.out.print(list.next()+", ");
//        }
//        System.out.println();
	}

}

@Entity
class TestEntity{
	@PrimaryKey
	int key;
	
	TestEntity(){}
	TestEntity(int key){
		this.key = key;
	}
	
	public String value(){
		return "tiesheng="+key;
	}
}